package com.dc.bobaoge.project.v3.to.v2.business.core;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreUser;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreUserMapper;
import com.own.business.sync.trigger.base.entity.SyncTrigger;
import com.own.business.sync.trigger.base.method.ResolveMethod;
import com.own.business.system.account.common.em.AccountType;
import com.own.business.system.account.entity.SystemAccount;
import com.own.business.system.account.mapper.SystemAccountMapper;
import com.own.business.system.user.entity.po.SystemUserInfo;
import com.own.business.system.user.mapper.SystemUserInfoMapper;
import com.own.business.system.user.mapper.SystemUserMapper;
import com.own.component.common.exception.BusinessSimpleException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * SystemUserResolveMethod
 *
 * @author chenxueli
 * @date 2024-01-16 19:13:47
 */
@Slf4j
@Component
public class SystemUserResolveMethod implements ResolveMethod {

    @Resource
    private SystemUserInfoMapper systemUserInfoMapper;
    @Resource
    private SystemUserMapper systemUserMapper;
    @Resource
    private SystemAccountMapper systemAccountMapper;

    @Resource
    private OldCoreUserMapper oldCoreUserMapper;

    @Resource
    private SystemUserStateResolveMethod systemUserStateResolveMethod;

    /**
     * 修改的处理方法
     *
     * @param trigger 日志信息
     */
    @Override
    public void update(SyncTrigger trigger) {
        throw new BusinessSimpleException("不支持该操作");
    }

    /**
     * 新增的处理方法
     * <p>
     * 新增用户的时候不存在用户的额外信息
     *
     * @param trigger 日志信息
     */
    @Override
    public void add(SyncTrigger trigger) {
        var user = systemUserMapper.selectById(trigger.getTargetId());
        if (user == null) {
            throw new BusinessSimpleException("未查询到用户信息");
        }
        // 判断是否存在了
        if (oldCoreUserMapper.selectById(trigger.getTargetId()) != null) {
            return;
        }
        // 查询用户详细信息
        var wrapperUserInfo = new LambdaUpdateWrapper<SystemUserInfo>().eq(SystemUserInfo::getFkUserId, user.getId());
        var userInfo = systemUserInfoMapper.selectOne(wrapperUserInfo);
        if (userInfo == null) {
            throw new BusinessSimpleException("未查询到用户详细信息");
        }
        // 查询用户的账号信息
        var wrapperAccount = new LambdaUpdateWrapper<SystemAccount>().eq(SystemAccount::getFkUserId, user.getId());
        var accountList = systemAccountMapper.selectList(wrapperAccount);
        // 获取账号
        var account = accountList.stream().filter(item -> item.getType().equals(AccountType.TEXT.type())).findFirst().orElse(null);
        // 构造信息
        var oldCoreUser = new OldCoreUser();
        oldCoreUser.setUserId(trigger.getTargetId());
        oldCoreUser.setUserAccount(account == null ? "" : account.getAccount());
        oldCoreUser.setUserPhone(userInfo.getPhone());
        oldCoreUser.setUserPwd(account == null ? "" : account.getPassword());
        oldCoreUser.setUserState(systemUserStateResolveMethod.getState(user));
        oldCoreUser.setUserRegTime(user.getGmtCreate());
        oldCoreUser.setUserLoginTime(user.getGmtCreate());
        oldCoreUser.setUserPayPwd("");
        oldCoreUser.setUserWeixin("");
        oldCoreUserMapper.insert(oldCoreUser);
    }

    /**
     * 名称信息
     *
     * @return 名称
     */
    @Override
    public String name() {
        return "system_user";
    }

}
